﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SpendAll(int pId)
    {
        string command = "";
        if (UserDefinedFunctions.PlayerHasSpecialBulding(pId, Database.SpecialBuldings.Market) == true)
            command = "Update players set Cash = Cash + (select sum(ProductionPoints) from planets where OwnerId = @pId) * @bonus where PlayerId = @pId";
        else
            command = "Update players set Cash = Cash + (select sum(ProductionPoints) from planets where OwnerId = @pId) where PlayerId = @pId";
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
    
            using (SqlCommand cmd = new SqlCommand(command, conn))
            {
                cmd.Parameters.Add("@bonus", SqlDbType.Float).Value = 1.05;
                cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "Update planets set ProductionPoints = 0 where OwnerId = @pId";
                cmd.ExecuteNonQuery();
            }
        }
    }
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SendMoneyToCapital(int pId)
    {
        int sId, plId;
        UserDefinedFunctions.PlayerCapital(pId, out sId, out plId);
        double cash = UserDefinedFunctions.GetPlayerFloat(pId, "Cash").Value * 0.952;
        if (sId == -1 || plId == -1) return;

        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Update players set Cash = 0 Where PlayerId = @pId", conn))
            {
                cmd.Parameters.Add("@pId", SqlDbType.Int).Value = pId;
                cmd.ExecuteNonQuery();
                cmd.CommandText = "Update planets set ProductionPoints =ProductionPoints + @c where PlanetId = @plId and SystemId = @sId";
                cmd.Parameters.Add("@c", SqlDbType.Float).Value = cash;
                cmd.Parameters.Add("@sId", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@plId", SqlDbType.Int).Value = plId;
                cmd.ExecuteNonQuery();
            }
        }
    }
};
